package com.szyjjk.config;

import java.util.Arrays;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;


/**  
* Title: LogAspects
* Description:  切面类 
* @author dingxy  
*/  

@Aspect
public class LogAspects {
	
	private static Logger logger =  LoggerFactory.getLogger(LogAspects.class);
	
	@Pointcut("!execution(* com.szyjjk.service..*.get*(..)) && !execution(* com.szyjjk.service..*.find*(..)) && execution(* com.szyjjk.service..*.*(..))")
	public void pointCut(){};
	
	@Before("pointCut()")
	public void logStart(JoinPoint joinPoint){
		
		Object[] args = joinPoint.getArgs();
		//System.out.println("---> "+joinPoint.getSignature());
		//System.out.println(""+joinPoint.getSignature().getName()+"运行。。。@Before:参数列表是：{"+Arrays.asList(args)+"}");
		
		// 接收到请求，记录请求内容
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        
        if(attributes!=null){
        	HttpServletRequest request = attributes.getRequest();
            // 记录下请求内容
            logger.info("URL : " + request.getRequestURL().toString());
            logger.info("HTTP_METHOD : " + request.getMethod());
            logger.info("IP : " + request.getRemoteAddr());
            logger.info("CLASS_METHOD : " + joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName());
            logger.info("ARGS : " + Arrays.toString(joinPoint.getArgs()));
        }
		
	}
	
}
